package mybatis.plus.injection;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.function.ConverterFileName;
import mybatis.plus.generator.CodeGenerator;

import java.sql.SQLException;
import java.util.HashMap;

/**
 * 代码生成
 */
public class StarDocuSignInjectionGenerator {

    /**
     * 执行 run
     */
    public static void main(String[] args) throws SQLException {
        String pkgPath = "/Users/cool/starProjects/star-docusign";
        FastAutoGenerator.create(CodeGenerator.DOCUSIGN_URL, CodeGenerator.DOCUSIGN_URL_USERNAME, CodeGenerator.DOCUSIGN_URL_PASSWORD)
                // 全局配置
                .globalConfig(builder -> builder.outputDir(pkgPath).author("ypf")
                        .disableOpenDir().fileOverride().enableSpringdoc())
                // 包配置
                .packageConfig(builder -> builder.parent("com.starcharge.overseas.star.docusign")
                        .controller("web.controller").service("core").serviceImpl("core.impl").mapper("dao.mapper").xml("dao.mapper").entity("dao.entity")
                        .pathInfo(new HashMap<OutputFile, String>() {
                            {
                                put(OutputFile.xml,"/Users/cool/starProjects/star-docusign/star-docusign-dao/src/main/resources/com/starcharge/overseas/star/docusign/dao");
                                put(OutputFile.entity,"/Users/cool/starProjects/star-docusign/star-docusign-dao/src/main/java/com/starcharge/overseas/star/docusign/dao/entity");
                                put(OutputFile.mapper,"/Users/cool/starProjects/star-docusign/star-docusign-dao/src/main/java/com/starcharge/overseas/star/docusign/dao/mapper");
                                put(OutputFile.service,"/Users/cool/starProjects/star-docusign/star-docusign-core/src/main/java/com/starcharge/overseas/star/docusign/core");
                                put(OutputFile.serviceImpl,"/Users/cool/starProjects/star-docusign/star-docusign-core/src/main/java/com/starcharge/overseas/star/docusign/core/impl");
                                put(OutputFile.controller,"/Users/cool/starProjects/star-docusign/star-docusign-web/src/main/java/com/starcharge/overseas/star/docusign/web/controller");
                            }
                        }))

                // 模板配置
//                .templateConfig(builder -> builder.disable(TemplateType.XML, TemplateType.CONTROLLER))
//                 注入配置
                .injectionConfig(builder -> builder.beforeOutputFile((tableInfo, objectMap) -> {
                            // 这里是预处理输出模板信息
                            System.out.println("tableInfo: " + tableInfo.getEntityName() + " objectMap: " + objectMap);
                        })
                        // 这里是自定义传入模板参数值
//                        .customMap(Collections.singletonMap("test", "baomidou"))
//                        .customFile(new HashMap<String, String>() {{
//                            // 这里可以自定义自己的模板
//                            put("hi.txt", "/templates/hi.vm");
//                            put("sample.vue", "/templates/vue.vm");
//                        }})
                        .build())
                // 策略配置 sample
                .strategyConfig((scanner, builder) -> builder.addInclude(scanner.apply("请输入表名？"))
                        .entityBuilder().enableLombok().controllerBuilder().enableRestStyle().serviceBuilder().convertServiceFileName(new ConverterFileName() {
                            @Override
                            public  String convert(String entityName) {
                                return entityName+"Service";
                            }
                        }))
                // 模板引擎的某个方法
//                .templateEngine(new VelocityTemplateEngine() {
//                    @Override
//                    protected void outputCustomFile(Map<String, String> customFile, TableInfo tableInfo, Map<String, Object> objectMap) {
//                        String otherPath = getPathInfo(OutputFile.other);
//                        customFile.forEach((key, value) -> {
//                            String fileName = String.format((otherPath + File.separator + "%s"), key);
//                            outputFile(new File(fileName), objectMap, value);
//                        });                    }
//                })
                // 执行
                .execute();
    }
}
